package com.nuoniu.sibanyun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.SysUserRole;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

/**
 * <p>
 * 用户与角色关联表 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@Mapper
@Component
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

    @Delete("delete from sys_user_role where role_id = #{id}")
    void deleteByRole(Long id);

    /**
     * 获取用户角色id
     * @author 小啊刚
     * @date 2020/6/3
     */
    @Select("select ur.role_id roleId from sys_user_role ur left join sys_role sr on ur.role_id = sr.id where ur.user_id = #{userId} and sr.company_id = #{companyId}")
    List<Long> checkRoleId(Long userId, Integer companyId);

    /**
     * 获取用户指定角色
     * @author 小啊刚
     * @date 2020/7/10
     */
    @Select("select * from sys_user_role where user_id = #{userId} and role_id = #{roleId}")
    SysUserRole getUserAdminRole(Integer userId, int roleId);

    /**
     * 删除用户所有角色
     * @author 小啊刚
     * @date 2020-07-24
     */
    @Delete("DELETE FROM sys_user_role WHERE user_id = #{userId}")
    void deleteByUserId(Long userId);

    /**
     * 获取角色使用用户列表
     * @author 小啊刚
     * @date 2020-08-13
     */
    @Select("select * from sys_user_role where role_id = #{id}")
    List<SysUserRole> getByRoleId(Long id);

    /**
     * 同步系统角色
     * @author 小啊刚
     * @date 2020-08-27
     */
    @Insert("insert into sys_role (company_id, company_name, role_name, role_type, data_scope, create_by) " +
            "select #{companyId}, #{companyName}, role_name, role_type, data_scope, #{userId} from sys_role where company_id=1 and id!=1 and id!=9")
    void saveAll(Long companyId, String companyName, Long userId);
}
